package com.yeyks.commonReference.service.hotel.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.yeyks.common.constants.ConfigKey;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.commonReference.service.config.ConfigService;
import com.yeyks.commonReference.service.hotel.HotelSearchJipService;
import com.yeyks.commonReference.service.hotel.RoomInfoService;
import com.yeyks.commonReference.utils.ConfidentialOperationUtil;
import com.yeyks.hotel.dal.dao.HotelInfoMapper;
import com.yeyks.hotel.dal.dao.RoomCalendarJipInfoMapper;
import com.yeyks.hotel.dal.dao.RoomInfoMapper;
import com.yeyks.hotel.dal.domain.HotelInfo;
import com.yeyks.hotel.dal.domain.RoomCalendarJipInfo;
import com.yeyks.hotel.dal.domain.RoomInfo;
import com.yeyks.hotel.dto.HotelInfoC2bHotelVipSearchDto;
import com.yeyks.hotel.dto.HotelInfoC2bHotelVipSearchRoomDto;
import com.yeyks.hotel.param.HotelInfoC2bHotelVipSearchParam;
import com.yeyks.hotel.param.HotelInfoHotelSearchOrderHotelInfoParam;
import com.yeyks.hotel.param.HotelSearchJipAppendNewHotelListParam;
import com.yeyks.hotel.param.HotelSearchJipAppendPageListParam;
import com.yeyks.hotel.utils.HotelInfoUtil;
import com.yeyks.hotel.utils.HotelSearchUtil;
import com.yeyks.hotel.vo.C2bSelectRoomCalendarVipInfoForOrderVo;
import com.yeyks.hotel.vo.HotelSearchJipSelectRoomCalendarVipInfoForOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HotelSearchJipServiceImpl implements HotelSearchJipService {

    @Autowired
    private HotelInfoMapper hotelInfoMapper;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private RoomCalendarJipInfoMapper roomCalendarJipInfoMapper;
    @Autowired
    private ConfigService configService;

    //Jip酒店搜索:v3.5
    @Override
    public com.github.pagehelper.PageInfo<HotelInfoC2bHotelVipSearchDto> hotelSearch(HotelInfoC2bHotelVipSearchParam param) {
        //修正时间
        param.setCheckIn(DateUtil.beginOfDay(param.getCheckIn()));
        param.setCheckOut(DateUtil.beginOfDay(param.getCheckOut()));
        List<HotelInfoC2bHotelVipSearchDto> hotelInfoC2bHotelVipSearchDtos = null;
//        //时间修正
//        param.setCheckIn(HotelSearchUtil.fixDate(param.getCheckIn()));
//        param.setCheckOut(HotelSearchUtil.fixDate(param.getCheckOut()));
        //时间校验
        HotelSearchUtil.checkSearchDate(param.getCheckIn(), param.getCheckOut());
        //获取搜索配置
        Map<String, Object> map = JSONUtil.toBean(configService.getValue(ConfigKey.C2B_SEARCH_CONDITION), Map.class);
        param.setDistance(null != map.get("distance") ? (Integer) map.get("distance") : 5000);
        //搜索
        Integer count = 3;
        while (count-- > 0 && (null == hotelInfoC2bHotelVipSearchDtos || hotelInfoC2bHotelVipSearchDtos.size() == 0)) {
            PageHelper.startPage(param.getPageNum(), param.getPageSize());
            hotelInfoC2bHotelVipSearchDtos = hotelInfoMapper.jipHotelSearch(param);
            param.setDistance(param.getDistance() * 2);
        }
        //填充文字星级显示
        for (HotelInfoC2bHotelVipSearchDto hotelInfoC2bHotelVipSearchDto : hotelInfoC2bHotelVipSearchDtos) {
            hotelInfoC2bHotelVipSearchDto.setStar(HotelInfoUtil.getStartText(hotelInfoC2bHotelVipSearchDto.getStarLevel()));
        }
        //填充房间信息
        if (null != hotelInfoC2bHotelVipSearchDtos && hotelInfoC2bHotelVipSearchDtos.size() > 0) {
            //房间id列表
            ArrayList<Integer> roomIds = new ArrayList<>();
            for (HotelInfoC2bHotelVipSearchDto hotelInfoC2bHotelVipSearchDto : hotelInfoC2bHotelVipSearchDtos) {
                List<HotelInfoC2bHotelVipSearchRoomDto> hotelInfoC2bHotelVipSearchRoomDtoList = hotelInfoC2bHotelVipSearchDto.getRoomList();
                for (HotelInfoC2bHotelVipSearchRoomDto hotelInfoC2bHotelVipSearchRoomDto : hotelInfoC2bHotelVipSearchRoomDtoList) {
                    roomIds.add(hotelInfoC2bHotelVipSearchRoomDto.getRoomId());
                }
            }
            //查询房间信息
            List<RoomInfo> roomInfos = roomInfoService.list(new QueryWrapper<RoomInfo>().in(RoomInfo.ID, roomIds));
            //list转map
            HashMap<Integer, RoomInfo> integerRoomInfoHashMap = new HashMap<>();
            for (RoomInfo roomInfo : roomInfos) {
                ConfidentialOperationUtil.dealRoomInfo(roomInfo);
                integerRoomInfoHashMap.put(roomInfo.getId(), roomInfo);
            }
            //填充roomInfo
            for (HotelInfoC2bHotelVipSearchDto hotelInfoC2bHotelVipSearchDto : hotelInfoC2bHotelVipSearchDtos) {
                List<HotelInfoC2bHotelVipSearchRoomDto> hotelInfoC2bHotelVipSearchRoomDtoList = hotelInfoC2bHotelVipSearchDto.getRoomList();
                for (HotelInfoC2bHotelVipSearchRoomDto hotelInfoC2bHotelVipSearchRoomDto : hotelInfoC2bHotelVipSearchRoomDtoList) {
                    hotelInfoC2bHotelVipSearchRoomDto.setRoomInfo(integerRoomInfoHashMap.get(hotelInfoC2bHotelVipSearchRoomDto.getRoomId()));
                }
            }
        }
        com.github.pagehelper.PageInfo<HotelInfoC2bHotelVipSearchDto> hotelInfoC2bHotelVipSearchDtoPageInfo = new com.github.pagehelper.PageInfo<>(hotelInfoC2bHotelVipSearchDtos);
        return hotelInfoC2bHotelVipSearchDtoPageInfo;
    }

    //Jip订单房态信息:v3.5
    @Override
    public HotelSearchJipSelectRoomCalendarVipInfoForOrderVo selectRoomCalendarVipInfoForOrder(HotelInfoHotelSearchOrderHotelInfoParam param) {
        //修正时间
        param.setStartDate(DateUtil.beginOfDay(param.getStartDate()));
        param.setEndDate(DateUtil.beginOfDay(param.getEndDate()));
//        //时间修正
//        param.setStartDate(HotelSearchUtil.fixDate(param.getStartDate()));
//        param.setEndDate(HotelSearchUtil.fixDate(param.getEndDate()));
        //时间校验
        HotelSearchUtil.checkSearchDate(param.getStartDate(), param.getEndDate());
        RoomInfo roomInfo = roomInfoMapper.selectById(param.getRoomId());
        if (3 != roomInfo.getVipFlag() || 1 != roomInfo.getVipStatus()) {
            throw new ServiceException("该房间不是精选房间");
        }
        //去除信息
        ConfidentialOperationUtil.dealRoomInfo(roomInfo);
        HotelInfo hotelInfo = hotelInfoMapper.selectById(roomInfo.getHotelId());
        List<RoomCalendarJipInfo> roomCalendarVipInfos = roomCalendarJipInfoMapper.selectList(new QueryWrapper<RoomCalendarJipInfo>()
                .eq(RoomCalendarJipInfo.ROOM_ID, param.getRoomId())
                .ge(RoomCalendarJipInfo.DATE, param.getStartDate())
                .lt(RoomCalendarJipInfo.DATE, param.getEndDate()));
        if (null == roomCalendarVipInfos || roomCalendarVipInfos.size() == 0) {
            throw new ServiceException("房态信息不存在");
        }
        for (RoomCalendarJipInfo roomCalendarJipInfo : roomCalendarVipInfos) {
            //去除信息
            ConfidentialOperationUtil.dealRoomCalendarJipInfo(roomCalendarJipInfo);
        }
        HotelSearchJipSelectRoomCalendarVipInfoForOrderVo vo = new HotelSearchJipSelectRoomCalendarVipInfoForOrderVo();
        vo.setRoomInfo(roomInfo);
        vo.setHotelInfo(hotelInfo);
        vo.setRoomCalendarVipInfos(roomCalendarVipInfos);
        return vo;
    }

    //最新上新:v4.4.3
    @Override
    public com.github.pagehelper.PageInfo<HotelInfoC2bHotelVipSearchDto> newHotelList(HotelSearchJipAppendNewHotelListParam param) {
        //修正时间
        param.setCheckIn(DateUtil.beginOfDay(param.getCheckIn()));
        param.setCheckOut(DateUtil.beginOfDay(param.getCheckOut()));
        List<HotelInfoC2bHotelVipSearchDto> hotelInfoC2bHotelVipSearchDtos = null;
//        //时间修正
//        param.setCheckIn(HotelSearchUtil.fixDate(param.getCheckIn()));
//        param.setCheckOut(HotelSearchUtil.fixDate(param.getCheckOut()));
        //时间校验
        HotelSearchUtil.checkSearchDate(param.getCheckIn(), param.getCheckOut());
        //搜索
        hotelInfoC2bHotelVipSearchDtos = hotelInfoMapper.newHotelList(param);
        //填充文字星级显示
        for (HotelInfoC2bHotelVipSearchDto hotelInfoC2bHotelVipSearchDto : hotelInfoC2bHotelVipSearchDtos) {
            hotelInfoC2bHotelVipSearchDto.setStar(HotelInfoUtil.getStartText(hotelInfoC2bHotelVipSearchDto.getStarLevel()));
        }
        //填充房间信息
        if (null != hotelInfoC2bHotelVipSearchDtos && hotelInfoC2bHotelVipSearchDtos.size() > 0) {
            //房间id列表
            ArrayList<Integer> roomIds = new ArrayList<>();
            for (HotelInfoC2bHotelVipSearchDto hotelInfoC2bHotelVipSearchDto : hotelInfoC2bHotelVipSearchDtos) {
                List<HotelInfoC2bHotelVipSearchRoomDto> hotelInfoC2bHotelVipSearchRoomDtoList = hotelInfoC2bHotelVipSearchDto.getRoomList();
                for (HotelInfoC2bHotelVipSearchRoomDto hotelInfoC2bHotelVipSearchRoomDto : hotelInfoC2bHotelVipSearchRoomDtoList) {
                    roomIds.add(hotelInfoC2bHotelVipSearchRoomDto.getRoomId());
                }
            }
            //查询房间信息
            List<RoomInfo> roomInfos = roomInfoService.list(new QueryWrapper<RoomInfo>().in(RoomInfo.ID, roomIds));
            //list转map
            HashMap<Integer, RoomInfo> integerRoomInfoHashMap = new HashMap<>();
            for (RoomInfo roomInfo : roomInfos) {
                ConfidentialOperationUtil.dealRoomInfo(roomInfo);
                integerRoomInfoHashMap.put(roomInfo.getId(), roomInfo);
            }
            //填充roomInfo
            for (HotelInfoC2bHotelVipSearchDto hotelInfoC2bHotelVipSearchDto : hotelInfoC2bHotelVipSearchDtos) {
                List<HotelInfoC2bHotelVipSearchRoomDto> hotelInfoC2bHotelVipSearchRoomDtoList = hotelInfoC2bHotelVipSearchDto.getRoomList();
                for (HotelInfoC2bHotelVipSearchRoomDto hotelInfoC2bHotelVipSearchRoomDto : hotelInfoC2bHotelVipSearchRoomDtoList) {
                    hotelInfoC2bHotelVipSearchRoomDto.setRoomInfo(integerRoomInfoHashMap.get(hotelInfoC2bHotelVipSearchRoomDto.getRoomId()));
                }
            }
        }
        com.github.pagehelper.PageInfo<HotelInfoC2bHotelVipSearchDto> hotelInfoC2bHotelVipSearchDtoPageInfo = new com.github.pagehelper.PageInfo<>(hotelInfoC2bHotelVipSearchDtos);
        return hotelInfoC2bHotelVipSearchDtoPageInfo;
    }

}
